package com.talent.jiwen_teacher.http.common;

import android.support.annotation.NonNull;

import com.talent.jiwen_teacher.base.App;
import com.talent.jiwen_teacher.http.result.UpDataBean;
import com.talent.jiwen_teacher.http.result.UpDateResult;
import com.talent.jiwen_teacher.util.CommonUtils;
import com.talent.jiwen_teacher.util.GsonUtil;
import com.talent.jiwen_teacher.util.SPConstant;
import com.talent.jiwen_teacher.util.SPUtil;
import com.vector.update_app.HttpManager;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;
import rx.Observable;

/**
 * Created by qyqx on 2018/7/10.
 */

public class UpdateAppHttpUtil implements HttpManager {

    @Override
    public void asyncGet(@NonNull String url, @NonNull Map<String, String> params, @NonNull Callback callBack) {

    }

    @Override
    public void asyncPost(@NonNull String url, @NonNull Map<String, String> params, @NonNull final Callback callBack) {

        Observable observable = ApiFactory.getEducationApiSingleton().upDataApp(SPUtil.stringOut(SPConstant.TOKEN), 4);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<UpDataBean>(App.getInstance()) {
            @Override
            protected void _onNext(UpDataBean upDataBean) {
                if (upDataBean.getCurrent_version() > CommonUtils.getLocalVersion()) {
                    String response;
                    if (upDataBean.getUpdate_status() == 1) {//强制更新
                        UpDateResult result=new UpDateResult("Yes",upDataBean.getCurrent_version_name(),upDataBean.getVersion_update_address(),upDataBean.getUpdate_content(),upDataBean.getFileSize(),true,"");
                        response= GsonUtil.getInstance().toJson(result);
                    } else{
                        UpDateResult result=new UpDateResult("Yes",upDataBean.getCurrent_version_name(),upDataBean.getVersion_update_address(),upDataBean.getUpdate_content(),upDataBean.getFileSize(),false,"");
                        response= GsonUtil.getInstance().toJson(result);
                    }
                    callBack.onResponse(response);
                }else{
                    callBack.onError("已经是最新版本");
                }
            }

            @Override
            protected void _onError(String message) {
                callBack.onError(message);
            }
        } ,false);


    }

    @Override
    public void download(@NonNull String url, @NonNull final String path, @NonNull String fileName, @NonNull final FileCallback callback) {
        if(!url.contains("http://")){
            url="http://"+url;
        }
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new FileCallBack(path, fileName) {
                    @Override
                    public void inProgress(float progress, long total, int id) {
                        callback.onProgress(progress, total);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e, int id) {
                        callback.onError(validateError(e, response));
                    }

                    @Override
                    public void onResponse(File response, int id) {
                        callback.onResponse(response);

                    }

                    @Override
                    public void onBefore(Request request, int id) {
                        super.onBefore(request, id);
                        callback.onBefore();
                    }
                });
    }
}
